package com.dyzs.client;

import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

import com.dyzs.domain.Plan;
import com.dyzs.domain.PlanJourney;
import com.dyzs.domain.PlanMember;
import com.dyzs.util.Constants;
import com.dyzs.util.WSUtils;

public class PlanClient {
	public String updateListForBZ(String userId, String page) throws Exception {
		Map<String, Object> params = new TreeMap<String, Object>();
		params.put("arg0", userId);
		params.put("arg1", page);
		try {
			String resultXml = WSUtils.callWS("getPlanListForBZ", params,
					Constants.PlanURL);
			return resultXml;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	public Map<String, Object> pullListXmlForBZ(String resultXml)
			throws Exception {
		if (resultXml.equals("W")) {
			throw new Exception("服务端异常！");
		} else {
			List<Map<String, Object>> showList = null;
			Map<String, Object> fatherMap = null;
			Map<String, Object> childMap = null;
			try {
				ByteArrayInputStream bin = new ByteArrayInputStream(resultXml
						.getBytes());
				InputStreamReader in = new InputStreamReader(bin);
				XmlPullParser parser = Xml.newPullParser();
				parser.setInput(in);
				int event = parser.getEventType();
				while (event != XmlPullParser.END_DOCUMENT) {
					switch (event) {
					case XmlPullParser.START_DOCUMENT:
						fatherMap = new HashMap<String, Object>();
						break;
					case XmlPullParser.START_TAG:
						if ("totalNum".equals(parser.getName())) {
							fatherMap.put("totalNum", parser.nextText());
						} else if ("lists".equals(parser.getName())) {
							showList = new ArrayList<Map<String, Object>>();
						} else if ("item".equals(parser.getName())) {
							childMap = new HashMap<String, Object>();
						} else if (childMap != null) {
							if ("a".equals(parser.getName())) {
								childMap.put("plan_id", parser.nextText());
							} else if ("b".equals(parser.getName())) {
								childMap.put("plan_date", parser.nextText());
							} else if ("c".equals(parser.getName())) {
								childMap.put("line_name", parser.nextText());
							}
						}
						break;
					case XmlPullParser.END_TAG:
						if ("item".equals(parser.getName())) {
							showList.add(childMap);
							childMap = null;
						}
						if ("lists".equals(parser.getName())) {
							fatherMap.put("list", showList);
						}
						break;
					}
					event = parser.next();
				}
			} catch (Exception e) {
				e.printStackTrace();
				throw new Exception("解析失败！", e);
			}
			return fatherMap;
		}
	}

	public String confirm(String userId, String planId, String check)
			throws Exception {
		Map<String, Object> params = new TreeMap<String, Object>();
		params.put("arg0", userId);
		params.put("arg1", planId);
		params.put("arg2", check);
		try {
			String resultXml = WSUtils.callWS("confirm", params,
					Constants.PlanURL);
			return resultXml;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	// 计调单列表
	public String updatePlanList(String userId) throws Exception {
		Map<String, Object> params = new TreeMap<String, Object>();
		params.put("arg0", userId);
		try {
			String resultXml = WSUtils.callWS("getPlanList", params,
					Constants.PlanURL);
			return resultXml;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	// 行程信息列表
	public String updateJourneyList(String planId) throws Exception {
		Map<String, Object> params = new TreeMap<String, Object>();
		params.put("arg0", planId);
		try {
			String resultXml = WSUtils.callWS("getJourney", params,
					Constants.PlanURL);
			return resultXml;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	// 行程信息解析
	public List<PlanJourney> pullJourneyXml(String resultXml, String planId)
			throws Exception {
		if (resultXml.equals("W")) {
			throw new Exception("服务端异常！");
		} else {
			List<PlanJourney> showList = null;
			PlanJourney journey = null;
			try {
				ByteArrayInputStream bin = new ByteArrayInputStream(resultXml
						.getBytes());
				InputStreamReader in = new InputStreamReader(bin);
				XmlPullParser parser = Xml.newPullParser();
				parser.setInput(in);
				int event = parser.getEventType();
				while (event != XmlPullParser.END_DOCUMENT) {
					switch (event) {
					case XmlPullParser.START_DOCUMENT:
						showList = new ArrayList<PlanJourney>();
						break;
					case XmlPullParser.START_TAG:
						if ("item".equals(parser.getName())) {
							journey = new PlanJourney();
						} else if (journey != null) {
							if ("journey_day".equals(parser.getName())) {
								journey.setJourneyDay(parser.nextText());
							} else if ("journey_content".equals(parser
									.getName())) {
								journey.setContent(parser.nextText());
							}
						}
						break;
					case XmlPullParser.END_TAG:
						if ("item".equals(parser.getName())) {
							if (journey != null) {
								journey.setUserId(Constants.USER_Mobile);
								journey.setPlanId(planId);
								showList.add(journey);
								journey = null;
							}
						}
						break;
					}
					event = parser.next();
				}
			} catch (Exception e) {
				e.printStackTrace();
				throw new Exception("解析失败！", e);
			}
			return showList;
		}
	}

	// 计调单列表解析
	public List<Plan> pullPlanListXml(String resultXml) throws Exception {
		if (resultXml.equals("W")) {
			throw new Exception("服务端异常！");
		} else {
			List<Plan> showList = null;
			Plan plan = null;
			try {
				ByteArrayInputStream bin = new ByteArrayInputStream(resultXml
						.getBytes());
				InputStreamReader in = new InputStreamReader(bin);
				XmlPullParser parser = Xml.newPullParser();
				parser.setInput(in);
				int event = parser.getEventType();
				while (event != XmlPullParser.END_DOCUMENT) {
					switch (event) {
					case XmlPullParser.START_DOCUMENT:
						showList = new ArrayList<Plan>();
						break;
					case XmlPullParser.START_TAG:
						if ("item".equals(parser.getName())) {
							plan = new Plan();
						} else if (plan != null) {
							if ("line_product_name".equals(parser.getName())) {
								plan.setLineName(parser.nextText());
							} else if ("team_date".equals(parser.getName())) {
								plan.setPlanDate(parser.nextText());
							} else if ("is_check".equals(parser.getName())) {
								plan.setIsCheck(parser.nextText());
							} else if ("single_id".equals(parser.getName())) {
								plan.setPlanId(parser.nextText());
							} else if ("journey_num".equals(parser.getName())) {
								plan.setJourneyNum(parser.nextText());
							}

							else if ("adult_num".equals(parser.getName())) {
								plan.setAdultNum(parser.nextText());
							} else if ("children_num".equals(parser.getName())) {
								plan.setChildrenNum(parser.nextText());
							} else if ("toddlers_num".equals(parser.getName())) {
								plan.setToddlersNum(parser.nextText());
							} else if ("single_remark".equals(parser.getName())) {
								plan.setSingleRemark(parser.nextText());
							} else if ("city_name".equals(parser.getName())) {
								plan.setCityName(parser.nextText());
							}

						}
						break;
					case XmlPullParser.END_TAG:
						if ("item".equals(parser.getName())) {
							if (plan != null) {
								plan.setUserId(Constants.USER_Mobile);
								showList.add(plan);
								plan = null;
							}
						}
						break;
					}
					event = parser.next();
				}
			} catch (Exception e) {
				e.printStackTrace();
				throw new Exception("解析失败！", e);
			}
			return showList;
		}
	}

	// 团员

	public String updateMember(String planId) throws Exception {
		Map<String, Object> params = new TreeMap<String, Object>();
		params.put("arg0", planId);
		try {
			String resultXml = WSUtils.callWS("getMemeber", params,
					Constants.PlanURL);
			return resultXml;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	// 团员解析
	public List<PlanMember> pullMemberXml(String resultXml, String planId)
			throws Exception {
		if (resultXml.equals("W")) {
			throw new Exception("服务端异常！");
		} else {
			List<PlanMember> showList = null;
			PlanMember member = null;
			try {
				ByteArrayInputStream bin = new ByteArrayInputStream(resultXml
						.getBytes());
				InputStreamReader in = new InputStreamReader(bin);
				XmlPullParser parser = Xml.newPullParser();
				parser.setInput(in);
				int event = parser.getEventType();
				while (event != XmlPullParser.END_DOCUMENT) {
					switch (event) {
					case XmlPullParser.START_DOCUMENT:
						showList = new ArrayList<PlanMember>();
						break;
					case XmlPullParser.START_TAG:
						if ("item".equals(parser.getName())) {
							member = new PlanMember();
						} else if (member != null) {
							if ("members_id".equals(parser.getName())) {
								member.setMemberId(parser.nextText());
							} else if ("members_name".equals(parser.getName())) {
								member.setMemberName(parser.nextText());
							} else if ("members_sex".equals(parser.getName())) {
								member.setMemberSex(parser.nextText());
							} else if ("members_type".equals(parser.getName())) {
								member.setMemberType(parser.nextText());
							} else if ("members_identity_card".equals(parser
									.getName())) {
								member.setMemeberCard(parser.nextText());
							} else if ("members_tel".equals(parser.getName())) {
								member.setMemeberTel(parser.nextText());
							} else if ("members_remark"
									.equals(parser.getName())) {
								member.setMemeberRemark(parser.nextText());
							}
						}
						break;
					case XmlPullParser.END_TAG:
						if ("item".equals(parser.getName())) {
							if (member != null) {
								member.setUserId(Constants.USER_Mobile);
								member.setPlanId(planId);
								showList.add(member);
								member = null;
							}
						}
						break;
					}
					event = parser.next();
				}
			} catch (Exception e) {
				e.printStackTrace();
				throw new Exception("解析失败！", e);
			}
			return showList;
		}
	}

	// 供应商信息
	public String getSupplier(String planId) throws Exception {
		Map<String, Object> params = new TreeMap<String, Object>();
		params.put("arg0", planId);
		try {
			String resultXml = WSUtils.callWS("getSupplier", params,
					Constants.PlanURL);
			return resultXml;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	// 商家信息解析

	public List<Map<String, Object>> pullSupplier(String resultXml)
			throws Exception {
		if (resultXml.equals("W")) {
			throw new Exception("服务端异常！");
		} else {
			Map<String, Object> map = null;
			ArrayList<Map<String, Object>> list = null;
			try {
				ByteArrayInputStream bin = new ByteArrayInputStream(resultXml.getBytes());
				InputStreamReader in = new InputStreamReader(bin);
				XmlPullParser parser = Xml.newPullParser();
				parser.setInput(in);
				int event = parser.getEventType();
				while (event != XmlPullParser.END_DOCUMENT) {
					switch (event) {
					case XmlPullParser.START_DOCUMENT:
						list = new ArrayList<Map<String, Object>>();
						break;
					case XmlPullParser.START_TAG:
						if ("item".equals(parser.getName())) {
							map = new HashMap<String, Object>();
						} else if (map != null) {
							map.put(parser.getName(), parser.nextText());
						}
						break;
					case XmlPullParser.END_TAG:
						if ("item".equals(parser.getName())) {
							if (map != null) {
								list.add(map);
								map = null;
							}
							break;
						}
					}
					event = parser.next();
				}
			} catch (Exception e) {
				e.printStackTrace();
				throw new Exception("解析失败！", e);
			}
			return list;
		}
	}
	
	
	
	// 供应商信息
	public String getSightspot(String planId) throws Exception {
		Map<String, Object> params = new TreeMap<String, Object>();
		params.put("arg0", planId);
		try {
			String resultXml = WSUtils.callWS("getSightspot", params,Constants.PlanURL);
			return resultXml;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	// 商家信息解析

	public List<Map<String, Object>> pullSightspot(String resultXml)
			throws Exception {
		if (resultXml.equals("W")) {
			throw new Exception("服务端异常！");
		} else {
			Map<String, Object> map = null;
			ArrayList<Map<String, Object>> list = null;
			try {
				ByteArrayInputStream bin = new ByteArrayInputStream(resultXml.getBytes());
				InputStreamReader in = new InputStreamReader(bin);
				XmlPullParser parser = Xml.newPullParser();
				parser.setInput(in);
				int event = parser.getEventType();
				while (event != XmlPullParser.END_DOCUMENT) {
					switch (event) {
					case XmlPullParser.START_DOCUMENT:
						list = new ArrayList<Map<String, Object>>();
						break;
					case XmlPullParser.START_TAG:
						if ("item".equals(parser.getName())) {
							map = new HashMap<String, Object>();
						} else if (map != null) {
							map.put(parser.getName(), parser.nextText());
						}
						break;
					case XmlPullParser.END_TAG:
						if ("item".equals(parser.getName())) {
							if (map != null) {
								list.add(map);
								map = null;
							}
							break;
						}
					}
					event = parser.next();
				}
			} catch (Exception e) {
				e.printStackTrace();
				throw new Exception("解析失败！", e);
			}
			return list;
		}
	}



}
